from django.shortcuts import render
from utils.logging import logger
from user.models import User
from libs.iPay import alipay
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from . import serializers
from rest_framework.generics import RetrieveAPIView
from rest_framework.response import Response
from . import models
from django.core.cache import cache
# Create your views here.


class AliPayAPIView(APIView):
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [IsAuthenticated]

    def post(self,request,*args,**kwargs):
        ser_obj = serializers.AliPaySerializers(data=request.data,context={'request':request})
        ser_obj.is_valid(raise_exception=True)
        ser_obj.save()
        return Response(ser_obj.pay_url)

    def get(self,request,*args,**kwargs):
        out_trade_no = request.query_params.get('out_trade_no')
        pay_url = cache.get(out_trade_no)
        if not pay_url:
            models.Order.objects.filter(out_trade_no=out_trade_no).update(order_status=3)
            return Response(data={
                'msg':'订单已超时',
                'status':300,
            })
        return Response(data={
            'out_trade_no': pay_url,
            'status': 200,
        })


class SuccessAPIView(AliPayAPIView):
    authentication_classes = []  ##局部禁用!!!!!!!
    permission_classes = []
    def patch(self, request, *args, **kwargs):
        # print(request.query_params)
        data = request.query_params.dict()
        sign = data.pop('sign')
        result = alipay.verify(data, sign)
        if result:
            # 一般不在同步回调直接操作订单状态
            pass
            # models.Order.objects.filter(out_trade_no=data.get('out_trade_no')).update(order_status=1)
        return Response('同步回调完成')

        # 异步支付宝回调接口：公网下才能验证

    def post(self, request, *args, **kwargs):
        data = request.data.dict()  # 回调参数，是QueryDict类型，不能直接调用pop方法
        sign = data.pop('sign')  # 签名
        out_trade_no = data.get('out_trade_no')  # 订单号
        result = alipay.verify(data, sign)
        if result and data["trade_status"] in ("TRADE_SUCCESS", "TRADE_FINISHED" ):
            try:
                order = models.Order.objects.get(out_trade_no=out_trade_no)
                if order.order_status != 1:
                    order.order_status = 1
                    order.save()
                    logger.error('%s订单完成支付' % out_trade_no)
                return Response('success')
            except:
                pass
        return Response('failed')


class DetailAPIView(RetrieveAPIView):
    queryset = User.objects.all()
    serializer_class = serializers.DetailModelSerializer